<?php

namespace common\models\book;

use Yii;
use common\helpers\StringHelper;

/**
 * This is the model class for table "{{%book}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $company_id 单位ID
 * @property string $company_name 单位
 * @property string|null $contact 参观联系人姓名
 * @property string|null $contact_mobile 参观联系人手机号
 * @property string|null $date 参观日期
 * @property int $room_id 座谈类型ID
 * @property int $waiter_department_id 对接部门ID
 * @property int $waiter_id 对接人ID
 * @property string|null $waiter_name 对接人姓名
 * @property int $numbers 参观人数
 * @property int $site_id 来源站点ID
 * @property string|null $mark 备注
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Book extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    /**
     * 状态
     */
    const STATUS_COMPLETE = 100;
    const STATUS_AUDITED = 20;
    const STATUS_PENDDING = 11;
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    const STATUS_CANCEL = -1;
    const STATUS_REJECT = -10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%book}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'company_id', 'room_id', 'waiter_department_id', 'waiter_id', 'numbers', 'site_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['company_name'], 'required'],
            [['date'], 'safe'],
            [['company_name'], 'string', 'max' => 100],
            [['contact', 'contact_mobile'], 'string', 'max' => 50],
            [['waiter_name', 'mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'company_id' => '单位ID',
            'company_name' => '单位',
            'contact' => '参观联系人姓名',
            'contact_mobile' => '参观联系人手机号',
            'date' => '参观日期',
            'room_id' => '座谈类型ID',
            'waiter_department_id' => '对接部门ID',
            'waiter_id' => '对接人ID',
            'waiter_name' => '对接人姓名',
            'numbers' => '参观人数',
            'site_id' => '来源站点ID',
            'mark' => '备注',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getSite() {
        return $this->hasOne(\common\models\site\Site::class, ['id' => 'site_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCompany() {
        return $this->hasOne(Company::class, ['id' => 'company_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getWaiter() {
        return $this->hasOne(BookWaiter::class, ['id' => 'waiter_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getWaiterDepartment() {
        return $this->hasOne(CompanyDepartment::class, ['id' => 'waiter_department_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getStaffs() {
        return $this->hasMany(BookStaff::class, ['book_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getRoomStages() {
        return $this->hasMany(RoomStage::class, ['book_id' => 'id'])->orderBy([
            'room_id' => SORT_DESC,
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getRooms() {
        return $this->hasMany(Room::class, ['id' => 'room_id'])->via('roomStages');
    }

    /**
     * {@inheritdoc}
     */
    public function getRoom() {
        return $this->hasOne(Room::class, ['id' => 'room_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getBookStages() {
        return $this->hasMany(BookStage::class, ['book_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getBookCategorys() {
        return $this->hasMany(BookCategory::class, ['book_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCategorys() {
        return $this->hasMany(Category::class, ['id' => 'category_id'])->via('bookCategorys');
    }

    /**
     * {@inheritdoc}
     */
    public function getStaffCount() {
        return $this->getStaffs()->active()->count();
    }

    /**
     * {@inheritdoc}
     */
    public function getContactMaskMobile() {
        return $this->contact_mobile ? StringHelper::maskChar($this->contact_mobile) : '-';
    }

    /**
     * 小程序跳转地址
     * @return bool
     */
    public function getMplink() {
        return Url::to(['/pages/show/index', 'id' => $this->id]);
    }

    /**
     * @inheritdoc
     */
    public function getState() {
        return $this->getMarkLabel('status');
    }

    /**
     * @inheritdoc
     */
    public function getWeekDate() {
        return $this->date ? Yii::$app->formatter->asWeekDate($this->date) : ' - ';
    }

    /**
     * @inheritdoc
     */
    public function getStagesText() {
        $stages = $this->bookStages;
        return \yii\helpers\ArrayHelper::getColumn($stages, 'duration');
    }

    /**
     * @inheritdoc
     */
    public function getCategorysText() {
        $categorys = $this->categorys;
        return \yii\helpers\ArrayHelper::getColumn($categorys, 'name');
    }

    /**
     * @inheritdoc
     */
    public function getVisitNumbers() {
        $numbers = $this->numbers ?: $this->staffCount;
        if (!$numbers) {
            return '参观人数：未设置';
        }
        return '参观人数:' . $numbers . '人';
    }

}
